package com.nycreativity.elecreativity.service.impl;

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.nycreativity.elecreativity.entity.enums.IsDelEnum
import com.nycreativity.elecreativity.entity.param.goodstype.GoodsTypeAddParam
import com.nycreativity.elecreativity.entity.param.goodstype.GoodsTypeUpdateParam
import com.nycreativity.elecreativity.entity.po.Goods
import com.nycreativity.elecreativity.entity.po.GoodsType
import com.nycreativity.elecreativity.entity.vo.goodstype.GoodsTypeVO
import com.nycreativity.elecreativity.mapper.GoodsMapper
import com.nycreativity.elecreativity.mapper.GoodsTypeMapper
import com.nycreativity.elecreativity.mapper.vomapper.GoodsTypeVOMapper
import com.nycreativity.elecreativity.service.GoodsTypeService
import com.nycreativity.elecreativity.util.MappingUtil
import com.nycreativity.elecreativity.util.usersession.UserContext
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import javax.annotation.Resource

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-11
 */
@Service
open class GoodsTypeServiceImpl : ServiceImpl<GoodsTypeMapper, GoodsType>(), GoodsTypeService {

    @Resource
    lateinit var goodsTypeMapper: GoodsTypeMapper
    @Resource
    lateinit var goodsTypeVOMapper: GoodsTypeVOMapper
    @Resource
    lateinit var goodsMapper: GoodsMapper

    override fun queryById(id: Long): GoodsTypeVO {
        return goodsTypeVOMapper.selectById(id)
    }

    override fun queryAll(): List<GoodsTypeVO> {
        return goodsTypeVOMapper.selectList(null)
    }

    override fun add(goodsTypeAddParam: GoodsTypeAddParam): Boolean {
        val goodsType = MappingUtil.getMappingObject(goodsTypeAddParam, GoodsType::class.java)
        if (goodsType == null || goodsType.isEmpty()){
            return false
        }
        goodsType.creator = UserContext.get()?.id
        goodsType.createTime = LocalDateTime.now()
        goodsTypeMapper.insert(goodsType)
        return true
    }

    override fun updateById(goodsTypeUpdateParam: GoodsTypeUpdateParam): Boolean {
        val goodsType = MappingUtil.getMappingObject(goodsTypeUpdateParam, GoodsType::class.java)
        if (goodsType == null || goodsType.isEmpty()){
            return false
        }
        goodsType.modifier = UserContext.get()?.id
        goodsType.modifyTime = LocalDateTime.now()
        goodsTypeMapper.updateById(goodsType)
        return true
    }

    override fun deleteById(id: Long): Boolean {

        //判断是否存在
        val goodsType = goodsTypeMapper.selectById(id) ?: return true

        //判断是否有goods还是该brand
        val queryWrapper = KtQueryWrapper(Goods::class.java).eq(Goods::typeId, id)
        val list = goodsMapper.selectList(queryWrapper)
        list?.let { if (it.isNotEmpty()) return false }

        val wrapper = KtUpdateWrapper(GoodsType::class.java)
                .set(GoodsType::isDel, IsDelEnum.YES.code)
                .set(GoodsType::modifier, UserContext.get()?.id)
                .set(GoodsType::modifyTime, LocalDateTime.now())
                .eq(GoodsType::id, id)
        goodsTypeMapper.update(null, wrapper)

        return true
    }

}
